package com.suning.sawp.service.impl.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapreduce.MapReduceResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.suning.sawp.dto.promotion.CouponSendRecordBean;
import com.suning.sawp.dto.promotion.StatisCouponSendRecordBean;
import com.suning.sawp.intf.dao.PromotionActivityDaoService;
import com.suning.sawp.po.promotion.CouponRecord;
import com.suning.sawp.service.util.DJStringUtils;

/**
 * 
 * 发券记录列表数据库操作<br> 
 * 〈功能详细描述〉
 *
 * @author 14062606
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class PromotionActivityDaoServiceImpl implements PromotionActivityDaoService{

	@Resource
    MongoOperations mongoTemplate;
	
	@Override
	public int countCouponRecordForPage(Date startTime, Date endTime,
			String regionCode, String storeCode, String activityName) {
		// 初始化db查询参数
        Query query = new Query();
        Criteria criteria = new Criteria();
        
        criteria.and(CouponRecord.Attributes.SEND_TIME)
			.gte(startTime).lte(endTime).and(CouponRecord.Attributes.STORE_CODE).exists(true);
        if (StringUtils.isNotBlank(activityName)){
            criteria.and(CouponRecord.Attributes.ACTIVITY_NAME).regex(DJStringUtils.escapeExprSpecialWord(activityName));
        }
        if (StringUtils.isNotBlank(regionCode)){
            criteria.and(CouponRecord.Attributes.REGION_CODE).is(regionCode);
        }
        if (StringUtils.isNotBlank(storeCode)){
        	criteria.and(CouponRecord.Attributes.STORE_CODE).is(storeCode);
        }
        
        query.addCriteria(criteria);
        
        return (int) mongoTemplate.count(query, CouponRecord.class);
	}

	@Override
	public List<CouponSendRecordBean> queryCouponRecordForPage(Date startTime, Date endTime, 
			String regionCode, String storeCode, String activityName, Integer startIndex,
			Integer limit) {
		List<CouponSendRecordBean> list = new ArrayList<CouponSendRecordBean>();
		Query query = new Query();
        Criteria criteria = new Criteria();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTime);
        calendar.add(Calendar.DATE, 1);
        criteria.and(CouponRecord.Attributes.SEND_TIME)
    		.gte(startTime).lte(calendar.getTime());
       
        if (StringUtils.isNotBlank(activityName)){
            criteria.and(CouponRecord.Attributes.ACTIVITY_NAME).regex(DJStringUtils.escapeExprSpecialWord(activityName));
        }
        if (StringUtils.isNotBlank(regionCode)){
            criteria.and(CouponRecord.Attributes.REGION_CODE).is(regionCode);
        }
        if (StringUtils.isNotBlank(storeCode)){
        	criteria.and(CouponRecord.Attributes.STORE_CODE).is(storeCode);
        }
        
        query.addCriteria(criteria);
        
        String mapFunc = "function(){emit(this.storeCode+this.activityId, {activityName:this.activityName,regionName:this.regionName,storeName:this.storeName,couponCount:1});}";
        String reduceFunc = "function (key, value) {var result = {activityName:'',regionName:'',storeName:'',couponCount:0};"+
        "for  ( var  i=0;i<value.length;i++) {"+
        "result.activityName= value[i].activityName;"+
        "result.regionName= value[i].regionName;"+
        "result.storeName= value[i].storeName;"+
        "result.couponCount += value[i].couponCount;"+
        "}"+
        "return result;}";
        MapReduceResults<StatisCouponSendRecordBean> mapReduce = mongoTemplate.mapReduce(query,"CouponRecord",mapFunc, reduceFunc,StatisCouponSendRecordBean.class);
        if(mapReduce != null){
            for(StatisCouponSendRecordBean coupon : mapReduce){
               list.add(coupon.getValue());
            }
        }
        
        return list;
	}

}
